import dataPreferences from '@ohos.data.preferences';
const PREFERENCES_NAME = 'myPreferences'; // 首选项名字
const KEY_APP_FONT_SIZE = 'appFontSize';  // 首选项Key字段
export class PreferencesUtil {
  createFontPreferences(context) {
    globalThis.getFontPreferences = (() => {
        let preferences: Promise<dataPreferences.Preferences> = dataPreferences.getPreferences(context, PREFERENCES_NAME);
        return preferences;
    });
  }
  saveDefaultFontSize(fontSize: number) {
    globalThis.getFontPreferences().then((preferences) => {
      // 判断保存的key是否存在
      preferences.has(KEY_APP_FONT_SIZE).then(async (isExist) => {
        if (!isExist) {
          // 保存数据
          await preferences.put(KEY_APP_FONT_SIZE, fontSize);
          preferences.flush();
        }
      }).catch((err) => {

      });
    }).catch((err) => {

    });
  }
}
export default new PreferencesUtil();